<template>
  <div class="siteProduct-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-button type="primary" icon="el-icon-edit" @click="handleAdd('editForm')">
          添加
        </el-button>
        <el-input v-model="search" placeholder="标题" style="width: 200px;padding-left: 20px" />
      </div>
      <el-table
        :data="Product.filter(data => !search || data.title.toLowerCase().includes(search.toLowerCase()))"
        style="width: 100%"
        class="list-table"
      >
        <el-table-column
          prop="contentId"
          label="ID"
          width="180"
        />
        <el-table-column
          prop="title"
          label="标题"
          width="180"
        />
        <el-table-column
          prop="suitable"
          label="适用对象"
          width="180"
        />
        <el-table-column
          prop="deadline"
          label="保理期限"
          width="180"
        />
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row.contentId)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- <div class="block">
        <el-pagination
          layout="prev, pager, next"
          :total="totalpages"
          background
          @current-change="onCurrentChange"
        />
      </div> -->
    </el-card>

    <el-dialog :title="titleMap[dialogStatus]" :visible.sync="dialogForm">
      <el-form :model="form">
        <el-form-item label="标题" :label-width="formLabelWidth">
          <el-input v-model="editForm.title" autocomplete="off" />
        </el-form-item>
        <el-form-item label="适用对象" :label-width="formLabelWidth">
          <el-input v-model="editForm.suitable" autocomplete="off" />
        </el-form-item>
        <el-form-item label="保理期限" :label-width="formLabelWidth">
          <el-input v-model="editForm.deadline" autocomplete="off" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="handleCancel('editForm')">取 消</el-button>
        <el-button type="primary" @click.native="handleUpdate('editForm')">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>

import {
  getProduct,
  addProduct,
  modifyProduct,
  deleteProduct
} from '@/api/Sitesetting'

export default {
  data() {
    return {
      Product: [],
      search: '',
      dialogForm: false,
      editForm: {
        contentId: '',
        title: '',
        suitable: '',
        deadline: ''
      },
      titleMap: {
        add: '新建',
        edit: '编辑'
      },
      dialogStatus: '',
      flag: 0
    }
  },

  created() {
    this.loadProduct()
    this.flag = 0
  },

  methods: {
    loadProduct() {
      getProduct().then(res => {
        console.log(res)
        this.Product = res.data
      })
    },

    handleAdd: function(editForm) {
      this.dialogForm = true
      this.dialogStatus = 'add'
      this.flag = 1
      this.editForm.title = editForm.title
      this.editForm.suitable = editForm.suitable
      this.editForm.deadline = editForm.deadline
    },

    handleEdit: function(index, row) {
      console.log(row)
      this.dialogForm = true
      this.editForm = Object.assign({}, row)
      this.dialogStatus = 'edit'
      this.flag = 0
    },

    handleUpdate(forName) {
      if (this.flag === 1) {
        addProduct(this.editForm).then(res => {
          console.log(this.editForm)
          console.log(res)
          this.loadProduct()
        })
        this.$message({
          type: 'success',
          message: '创建成功'
        })
      } else if (this.flag === 0) {
        modifyProduct(this.editForm).then(res => {
          console.log(res)
          this.loadProduct()
        })
        this.$message({
          type: 'success',
          message: '修改成功'
        })
      }
      this.dialogForm = false
    },

    handleCancel(formName) {
      this.$message({
        type: 'info',
        message: '已取消'
      })
      this.dialogForm = false
    },

    handleDelete: function(contentId) {
      // 删除
      this.$confirm('确认删除?', '删除提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 确认执行
        console.log(contentId)
        new Promise((resolve, reject) => {
          deleteProduct({ contentId: contentId }).then(res => {
          // 删除成功
            this.loadProduct()
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }

  }
}
</script>

<style>

</style>
